package com.ruoyi.fly.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fly.domain.*;
import com.ruoyi.fly.domain.vo.AppletMemberSearchVo;
import com.ruoyi.fly.domain.vo.AppletMemberVo;
import com.ruoyi.fly.domain.vo.CeresMemberVo;
import com.ruoyi.fly.domain.vo.RecommenderVo;
import com.ruoyi.fly.mapper.AppletMemberMapper;
import com.ruoyi.fly.mapper.StoreMapper;
import com.ruoyi.fly.mapper.VehiclesMapper;
import com.ruoyi.fly.service.AppletMemberService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 商城会员信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class AppletMemberServiceImpl implements AppletMemberService {
    @Autowired
    private AppletMemberMapper appletMemberMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private VehiclesMapper vehiclesMapper;

    /**
     * 查询商城会员信息
     *
     * @param id 商城会员信息主键
     * @return 商城会员信息
     */
    @Override
    public CeresMember selectAppletMemberById(String id) {
        return appletMemberMapper.selectAppletMemberById(id);
    }

    /**
     * 查询推荐人信息
     * @param recommenderVo
     * @return
     */
    @Override
    public R selectRecommentderList(RecommenderVo recommenderVo) {
        PageHelper.startPage(recommenderVo.getPageNum(), recommenderVo.getPageSize());
        List<Recommender> recommenders = appletMemberMapper.selectRecommenderList(recommenderVo);
        PageInfo<Recommender> pageInfo = new PageInfo<>(recommenders);
        return R.ok(pageInfo);
    }

    /**
     * 查询商城会员信息列表
     *
     * @param appletMember 商城会员信息
     * @return 商城会员信息
     */
    @Override
    public List<CeresMember> selectAppletMemberList(AppletMemberSearchVo appletMember) {
        return appletMemberMapper.selectAppletMemberList(appletMember);
    }

    @Override
    public List<CeresMemberVo> selectAppletMemberList1(AppletMemberSearchVo appletMember) {
        List<CeresMember> ceresMembers = appletMemberMapper.selectAppletMemberList(appletMember);
        List<CeresMemberVo> ceresMemberVos = new ArrayList<>();
        ceresMembers.forEach(ceresMember -> {
            CeresMemberVo ceresMemberVo = new CeresMemberVo();
            if (ceresMember.getRoleType() != null) {
                //BeanUtils.copyProperties(ceresMember, ceresMemberVo);
                if (ceresMember.getRoleType() == 0) {
                    if (ceresMember.getId() != null){
                        Store store = storeMapper.selectStoreByMid(ceresMember.getId());
                        if (store!=null) {
                            ceresMemberVo.setStoreName(store.getStoreName());
                        }
                    }else {
                        ceresMemberVo.setStoreName(null);
                    }
                    ceresMemberVo.setLicensePlate(null);
                    ceresMemberVo.setNickname(ceresMember.getNickname());
                    ceresMemberVo.setRoleType(ceresMember.getRoleType());
                    ceresMemberVo.setHeadimg(ceresMember.getHeadimg());
                    ceresMemberVo.setPhone(ceresMember.getPhone());
                    ceresMemberVo.setReferPhone(ceresMember.getReferPhone());
                    ceresMemberVo.setCreateTime(ceresMember.getCreateTime());
                } else if (ceresMember.getRoleType() == 1) {
                    if (ceresMember.getId() != null){
                        Vehicles vehicles = vehiclesMapper.selectPcVehiclesByMid(ceresMember.getId());
                        if (vehicles != null){
                            ceresMemberVo.setLicensePlate(vehicles.getLicensePlate());
                        }
                    }else {
                        ceresMemberVo.setLicensePlate(null);
                    }
                    ceresMemberVo.setStoreName(null);
                    ceresMemberVo.setNickname(ceresMember.getNickname());
                    ceresMemberVo.setRoleType(ceresMember.getRoleType());
                    ceresMemberVo.setHeadimg(ceresMember.getHeadimg());
                    ceresMemberVo.setPhone(ceresMember.getPhone());
                    ceresMemberVo.setReferPhone(ceresMember.getReferPhone());
                    ceresMemberVo.setCreateTime(ceresMember.getCreateTime());
                }
            } else {
                ceresMemberVo.setStoreName(null);
                ceresMemberVo.setLicensePlate(null);
                ceresMemberVo.setNickname(ceresMember.getNickname());
                ceresMemberVo.setRoleType(ceresMember.getRoleType());
                ceresMemberVo.setHeadimg(ceresMember.getHeadimg());
                ceresMemberVo.setPhone(ceresMember.getPhone());
                ceresMemberVo.setReferPhone(ceresMember.getReferPhone());
                ceresMemberVo.setCreateTime(ceresMember.getCreateTime());
            }
            ceresMemberVos.add(ceresMemberVo);
        });
        return ceresMemberVos;
    }


//    /**
//     * 新增商城会员信息
//     *
//     * @param appletMember 商城会员信息
//     * @return 结果
//     */
//    @Override
//    public int insertAppletMember(AppletMember appletMember)
//    {
//        appletMember.setCreateTime(DateUtils.getNowDate());
//        return appletMemberMapper.insertAppletMember(appletMember);
//    }

    /**
     * 修改商城会员信息
     *
     * @param appletMember 商城会员信息
     * @return 结果
     */
    @Override
    public R updateAppletMember(AppletMember appletMember) {
        appletMember.setUpdateUser(Long.valueOf(String.valueOf(SecurityUtils.getUserId())));
        return appletMemberMapper.updateAppletMember(appletMember) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除商城会员信息信息
     *
     * @param id 商城会员信息主键
     * @return 结果
     */
    @Override
    public R deleteAppletMemberById(String id) {
        return appletMemberMapper.deleteAppletMemberById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteAppletMemberByIds(String[] ids) {
        return appletMemberMapper.deleteAppletMemberByIds(ids) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
